package com.cswangdao.service.impl;

import com.cswangdao.domain.po.MarketSystem;
import com.cswangdao.mapper.MarketSystemMapper;
import com.cswangdao.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class SystemServiceImpl implements SystemService {

    @Autowired
    private MarketSystemMapper systemMapper;

    @Override
    public Map<String,Object> getCofigExpress(String prefix) {
        Map<String,Object> map = new HashMap<>();
        List<MarketSystem> systemList =systemMapper.getByPrefix(prefix);
        for (MarketSystem system : systemList) {
            map.put(system.getKeyName(), system.getKeyValue());
        }
        return map;
    }

    @Override
    public void updateBatch(Map<String, Object> map,String prefix) {
        List<MarketSystem> marketSystemList = new ArrayList<>();
        Date now = new Date();
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            MarketSystem marketSystem = new MarketSystem();
            marketSystem.setKeyValue(entry.getValue().toString());
            marketSystem.setKeyName(entry.getKey());
            marketSystem.setUpdateTime(now);
            marketSystem.setAddTime(now);
            marketSystemList.add(marketSystem);
        }
        systemMapper.deleteByPrefix(prefix);
        systemMapper.insertBatch(marketSystemList);

    }
}
